package com.sunshine.lnuplusweb.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器，用于拦截没有登录时的非法访问
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = (String) request.getSession().getAttribute("userId");
        if (null == userId) {
            //得重定向到登录，要不会就会空白
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }
        else return true;
    }
}
